package cn.waqwb.queque;

import javax.sound.midi.Soundbank;
import java.util.Scanner;

/**
 * @author JokerQ
 * @date 2019/9/22 - 17:41
 */
public class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(3);
        char key = ' ';//接收用户输入
        Scanner sc = new Scanner(System.in);
        boolean loop = true;
        //输出一个菜单
        while (loop){
            System.out.println("s(show):显示队列");
            System.out.println("e(exit):退出程序");
            System.out.println("a(add):添加数据到队列");
            System.out.println("g(get):从队列取出数据");
            System.out.println("h(head):查看队列头数据");
            key = sc.next().charAt(0);
            switch (key){
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    int val = sc.nextInt();
                    arrayQueue.addQueue(val);
                    break;
                case 'g':
                    try {
                        int res = arrayQueue.getQueue();
                        System.out.println("取出的数据是："+res);
                    } catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = arrayQueue.headQueue();
                        System.out.println("队列头的数据是:"+res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    sc.close();
                    loop = false;
                    break;

            }
        }
        System.out.println("程序推出！");
    }
}
//使用数组模拟队列
class ArrayQueue {
    //数组最大容量
    private int maxSize;
    //队列头
    private int front;
    //队列尾
    private int rear;
    //存放数据，模拟队列
    private int[] arr;

    public  ArrayQueue(int arrMaxSize){
        maxSize = arrMaxSize;
        arr = new int[maxSize];
        //指向队列头部，分析书front是指向队列头的前一个位置
        front = -1;
        //指向队列尾
        rear = -1;
    }
    public boolean isFull(){
        return rear== (maxSize-1);
    }
    public boolean isEnpty(){
        return rear == front;
    }
    public void  addQueue(int n ){
        if (isFull()){
            System.out.println("队列满，不能加入数据");
            return;
        }
        //让rear后移
        rear++;
        arr[rear] = n;
    }
    public int getQueue(){
        if (isEnpty()){
            throw new RuntimeException("队列为空，不能取出数据！");
        }
        //后移front，证明加入数据
        front++;
        return arr[front];
    }
    //显示队列的所有数据
    public void showQueue(){
        if (isEnpty()){
            System.out.println("队列为空，没有数据");
            return;
        }
        for (int i=0;i<arr.length;i++){
            System.out.println("arr["+i+"]:"+arr[i]);
        }
    }
    //显示队列的头数据，只是显示并不是取出
    public int headQueue(){
        if (isEnpty()){
            throw new RuntimeException("队列为空，没有数据");
        }
        return arr[front+1];
    }
}
